package com.chaodev.base.config;

import com.alibaba.fastjson.JSONObject;
import com.chaodev.base.model.TokenInfo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@ConfigurationProperties(prefix = "config.jwt")
@Component
public class JwtConfig {
    /*
     * 根据身份ID标识，生成Token
     */
    public TokenInfo getToken (String Userid){
        Date nowDate = new Date();
        TokenInfo tokenInfo=new TokenInfo();
        //过期时间
        try {
            Date expireDate = new Date(nowDate.getTime() + expire * 1000);
            String token = Jwts.builder()
                    .setHeaderParam("typ", "JWT")
                    .setSubject(Userid)
                    .setIssuedAt(nowDate)
                    .setExpiration(expireDate)
                    .signWith(SignatureAlgorithm.HS512, secret)
                    .compact();
            tokenInfo.setToken(token);
            //tokenInfo.setUserId(Userid);
            //tokenInfo.setExpireDate(expireDate);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return tokenInfo;
    }

    public void ErrorResult(HttpServletResponse response)
    {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        JSONObject res = new JSONObject();
        res.put("result","ERROR");
        res.put("dsc","token过期");
        PrintWriter out = null ;
        try {
            out = response.getWriter();
        } catch (IOException e) {
//            e.printStackTrace();
        }
        out.write(res.toString());
        out.flush();
        out.close();
    }
    /*
     * 获取 Token 中注册信息
     */
    public Claims getTokenClaim (String token) {
        try {
            return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /*
     * Token 是否过期验证
     */
    public boolean isTokenExpired (Date expirationTime) {
        return expirationTime.before(new Date());
    }
    private String secret;
    private long expire;
    private long redisexp;
    private String header;
    public String getSecret() {
        return secret;
    }
    public void setSecret(String secret) {
        this.secret = secret;
    }
    public long getExpire() {
        return expire;
    }
    public void setExpire(long expire) {
        this.expire = expire;
    }
    public String getHeader() {
        return header;
    }
    public void setHeader(String header) {
        this.header = header;
    }

    public long getRedisexp() {
        return redisexp;
    }

    public void setRedisexp(long redisexp) {
        this.redisexp = redisexp;
    }
}
